/* 
* Author Sergey Makeev <mail@sergeymakeev.com>, Aug 2009.
*
*
* This software is 100% free
* You may not use this software to directly or indirectly cause harm to others.
* This software is provided as-is and without warrantee. Use at your own risk.
*/
#include "hikari.h"

reflection::Database::Database()
{
	root = NULL;
}

reflection::Database::~Database()
{
}

reflection::MetaData* reflection::Database::findClassMataDataByName(const char * szClassName, unsigned long len, unsigned int hash)
{
	reflection::MetaData* reflClass = getFirst();
	for (;;)
	{
		if (reflClass == NULL)
		{
			break;
		}

		const reflection::staticString & clName = reflClass->getClassName();
		if (clName.hash() == hash && clName.size() == len && (strcmp (clName.c_str(), szClassName) == 0))
		{
			return reflClass;
		}

		reflClass = reflClass->getNext();
	}

	return NULL;
}

reflection::MetaData* reflection::Database::getByClassStaticName(const reflection::staticString & className)
{
	return findClassMataDataByName(className.c_str(), className.size(), className.hash());
}

reflection::MetaData* reflection::Database::getByClassName(const char * szClassName)
{
	unsigned long len;
	unsigned int hash = reflection::StringHashRuntime(szClassName, len);
	return findClassMataDataByName(szClassName, len, hash);
}

void reflection::Database::registerClassMataData(reflection::MetaData * desc)
{
	desc->setNext(root);
	root = desc;
}

reflection::MetaData* reflection::Database::getFirst()
{
	return root;
}


base_rtti_object * reflection::Database::createByClassName(const char * szClassName)
{
	reflection::MetaData* metaData = getByClassName(szClassName);
	if (metaData)
	{
		return metaData->create();
	}

	return NULL;
}

